import axios from 'axios';
import utils from './utils';
import SEM from './semantic';
import _ from 'lodash';
import api from './api';
import config from './config.js';

//super error handles except these api;
const excepts = [];

const getRelativeUrl = (url) => {
    var str = url.replace(config.apiHost, '');
    for (let i in config.apiPrefixes) str = str.replace(config.apiPrefixes[i], '');
    return str;
};

const isExcept = (error) => {
    const exceptApis = excepts.map(f => api[f]());

    const apiUrl = getRelativeUrl(error.response.config.url);
    let arrApiUrl = apiUrl.split('/');
    arrApiUrl.shift();

    let is = false;

    console.log('exceptApis', exceptApis);
    for (let excApi of exceptApis) {
        var arrExcApi = excApi.split('/');
        arrExcApi.shift();

        for (let i in arrExcApi) {
            console.log(arrApiUrl[i], arrExcApi[i]);
            if (arrApiUrl[i] == arrExcApi[i] || /^:.*/.test(arrExcApi[i])) {
                is = true;
            } else {
                is = false;
                break;
            }
        }
        if (is) break;
    }

    return is;
};

const strategy = (error) => {
    console.log(isExcept(error));
    if (isExcept(error)) return;
    //return;
    console.log(error.response);
    return {
        [SEM.ERR_HAVE_NOT_SIGNIN]: (res) => {
            utils.delCookie('username', '');
            window.location.href = window.location.protocol + '//' + window.location.host;
        },
        [SEM.ERR_EMAIL_HAVE_REG]: (res) => {

        },
        [SEM.ERR_PASSWORD]: (res) => {

        },
        [SEM.ERR_RESOURCE_CANNOT_USE]: (res) => {
            window.location.href = $$.url('error', {
                type: SEM.ERR_RESOURCE_CANNOT_USE
            });
        },
        [SEM.ERR_ILLEGAL_ACCESS_TO_RESOURCE]: (res) => {
            window.location.href = $$.url('error', {
                type: SEM.ERR_ILLEGAL_ACCESS_TO_RESOURCE
            });
        },
        [SEM.ERR_USER_NOT_FOUND]: (res) => {
            window.location.href = $$.url('error', {
                type: SEM.ERR_USER_NOT_FOUND
            });
        },
        [SEM.ERR_RESOURCE_NOT_FOUND]: (res) => {
            window.location.href = $$.url('error', {
                type: SEM.ERR_RESOURCE_NOT_FOUND
            });

        },

        [SEM.ERR_NOT_FOUND]: (res) => {
            window.location.href = $$.url('error', {
                type: SEM.ERR_NOT_FOUND
            });

        },
        [SEM.ERR_INNER_ERROR]: (res) => {
            window.location.href = $$.url('error', {
                type: SEM.ERR_NOT_FOUND
            });

        },

    }[error.response.status](error.response);
};

const axiosIns = axios.create({
    baseURL: 'http://plant.realibox.com/api/v1',
    transformResponse: [(data) => {
        if (/<\?xml/.test(data)) {
            data = utils.xmlToJson(data);
        } else {
            data = JSON.parse(data);
        }
        return data;
    }]
});

axiosIns.rawAxios = axios;

axiosIns.interceptors.response.use(function(response) {
    // Do something with res data

    return response;
}, function(error) {

    strategy(error);
    // Do something with response error
    return Promise.reject(error);
});


//error strategy


export default axiosIns;
